namespace Microsoft.Robotics.Core.Algorithms
{
    /// <summary>
    /// Maintains the number of requests made and throttles frequency down given a scale down factor
    /// </summary>
    public class FrequencyThrottler
    {
        /// <summary>
        /// Frequency scale down factor
        /// </summary>
        private int frequencyScaleDownFactor;

        /// <summary>
        /// Number of requests made so far
        /// </summary>
        private int requestCount;

        /// <summary>
        /// Initializes a new instance of the <see cref="FrequencyThrottler" /> class.
        /// </summary>
        /// <param name="frequencyScaleDownFactor">Frequency scale down factor</param>
        public FrequencyThrottler(int frequencyScaleDownFactor) 
        {
            this.requestCount = 0;
            this.frequencyScaleDownFactor = frequencyScaleDownFactor;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FrequencyThrottler" /> class.
        /// </summary>
        /// <returns>true if event is to be passed through, false otherwise</returns>
        public bool Throttle()
        {
             ++this.requestCount;

            if (0 == this.requestCount % this.frequencyScaleDownFactor) 
            {
                return true;
            }

            return false;
        }
    }
}
